﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FirmaTransportowa
{
    public partial class DispatcherPackagesList : Form
    {
        SqlConnection dbConnection;

        public DispatcherPackagesList()
        {
            InitializeComponent();
            SqlConnector connection = new SqlConnector();
            dbConnection = connection.getInstance();
            displayPackages();
        }

        private void displayPackages()
        {
            SqlCommand command = new SqlCommand("SELECT p.package_ID, p.status, p.add_date, p.weight, s.city, r.city FROM (Package as p JOIN Senders as s ON s.sender_ID = p.sender_ID) JOIN Recipients as r ON r.recipient_ID = p.recipient_ID", dbConnection);

            SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
            DataTable dataTable = new DataTable();
            dataAdapter.Fill(dataTable);

            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = dataTable;
            packagesGridView.DataSource = bindingSource;
            packagesGridView.Columns[0].HeaderText = "ID";
            packagesGridView.Columns[1].HeaderText = "Status";
            packagesGridView.Columns[2].HeaderText = "Data dodania";
            packagesGridView.Columns[3].HeaderText = "Waga";
            packagesGridView.Columns[4].HeaderText = "Z";
            packagesGridView.Columns[5].HeaderText = "Do";
            packagesGridView.Columns[0].Width = 100;
            packagesGridView.Columns[1].Width = 70;
            packagesGridView.Columns[2].Width = 65;
            packagesGridView.Columns[3].Width = 40;
            packagesGridView.Columns[4].Width = 135;
            packagesGridView.Columns[5].Width = 135;
        }

        private void DispatcherPackagesList_Shown(object sender, EventArgs e)
        {
            try
            {
                dbConnection.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private void DispatcherPackagesList_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                dbConnection.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }


    }
}
